import React, { useImperativeHandle } from "react";
import { Form, Input, Radio, Select } from 'antd';

import { type IOuterCreateOrEditProps } from "@yaf/pro-ui";
import { getEnv } from "@/constants";

import { RespDataType } from "../../api";

const layout = {
  labelCol: { span: 6 },
  wrapperCol: { span: 13 },
};

export const getCreateOrEdit = (agentList: RespDataType.AgentSelectList) => {
  const { commonStatus, commonStatusList } = getEnv().globalConfig;

  const CreateOrEdit: IOuterCreateOrEditProps["CreateOrEditContent"] = React.forwardRef(({ values }, actionRef) => {
    const [form] = Form.useForm();
    useImperativeHandle(actionRef, () => ({
      getFormData: () => new Promise((resolve, reject) => {
        form.validateFields().then((data) => {
          resolve(data);
        }).catch(() => {
          reject(new Error("请检查参数"));
        });
      })
    }));

    return (
      <Form
        {...layout}
        initialValues={{
          status: commonStatus.normal,
          ...values
        }}
        form={form}
      >
        <Form.Item
          label="账户名称"
          name="name"
          rules={[{ required: true, message: '请填写账户名称!' }]}
        >
          <Input />
        </Form.Item>
        <Form.Item label="代理商" name="agentId" rules={[{ required: true, message: '请选择代理商!' }]}>
          <Select
            options={agentList}
            style={{ width: "100%" }}
            showSearch={true}
            filterOption={(inputValue, option) => {
              return option.label.toLowerCase().includes(inputValue.toLowerCase());
            }}
          />
        </Form.Item>
        <Form.Item label="公司地址" name="address">
          <Input />
        </Form.Item>
        <Form.Item label="公司联系人" name="linkman">
          <Input />
        </Form.Item>
        <Form.Item label="联系人手机号" name="linkmanPhone">
          <Input />
        </Form.Item>
        <Form.Item label="状态" name="status">
          <Radio.Group options={commonStatusList} />
        </Form.Item>
        <Form.Item label="备注" name="remark">
          <Input.TextArea />
        </Form.Item>
      </Form>
    )
  });

  return CreateOrEdit
}

